package com.apollographql.apollo.api;

import com.apollographql.apollo.subscription.ApolloOperationMessageSerializer;
import com.apollographql.apollo.subscription.OperationServerMessage;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.kt.Metadata;
import com.kt.TypeCastException;
import com.kt.collections.CollectionsKt;
import com.kt.collections.MapsKt;

@Metadata(bv = {1, 0, 3}, d1 = {"\u0000B\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010$\n\u0002\u0010\u000e\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\bÆ\u0002\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002JT\u0010\u0003\u001a\b\u0012\u0004\u0012\u0002H\u00050\u0004\"\b\b\u0000\u0010\u0006*\u00020\u0007\"\u0004\b\u0001\u0010\u00052\u0014\u0010\b\u001a\u0010\u0012\u0004\u0012\u00020\n\u0012\u0006\u0012\u0004\u0018\u00010\u00010\t2\u0016\u0010\u000b\u001a\u0012\u0012\u0004\u0012\u0002H\u0006\u0012\u0004\u0012\u0002H\u0005\u0012\u0002\b\u00030\f2\u0006\u0010\r\u001a\u00020\u000eH\u0002JF\u0010\u0003\u001a\b\u0012\u0004\u0012\u0002H\u00050\u0004\"\b\b\u0000\u0010\u0006*\u00020\u0007\"\u0004\b\u0001\u0010\u00052\u0006\u0010\u000f\u001a\u00020\u00102\u0016\u0010\u000b\u001a\u0012\u0012\u0004\u0012\u0002H\u0006\u0012\u0004\u0012\u0002H\u0005\u0012\u0002\b\u00030\f2\u0006\u0010\r\u001a\u00020\u000eH\u0007J\u001a\u0010\u0011\u001a\u00020\u0012*\u0010\u0012\u0004\u0012\u00020\n\u0012\u0006\u0012\u0004\u0018\u00010\u00010\tH\u0002J\u001c\u0010\u0013\u001a\u00020\u0014*\u0012\u0012\u0004\u0012\u00020\n\u0012\u0006\u0012\u0004\u0018\u00010\u0001\u0018\u00010\tH\u0002¨\u0006\u0015"}, d2 = {"Lcom/apollographql/apollo/api/internal/SimpleOperationResponseParser;", "", "()V", "parse", "Lcom/apollographql/apollo/api/Response;", "W", "D", "Lcom/apollographql/apollo/api/Operation$Data;", "response", "", "", "operation", "Lcom/apollographql/apollo/api/Operation;", "scalarTypeAdapters", "Lcom/apollographql/apollo/api/ScalarTypeAdapters;", "source", "Lokio/BufferedSource;", "readError", "Lcom/apollographql/apollo/api/Error;", "readErrorLocation", "Lcom/apollographql/apollo/api/Error$Location;", "apollo-api"}, k = 1, mv = {1, 4, 0})
/* compiled from: SimpleOperationResponseParser.kt */
public final class SimpleOperationResponseParser {
    public static final SimpleOperationResponseParser INSTANCE = new SimpleOperationResponseParser();

    private SimpleOperationResponseParser() {
    }

    /* JADX WARNING: Code restructure failed: missing block: B:13:0x0048, code lost:
        r3 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:15:?, code lost:
        r0.close();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:17:0x0053, code lost:
        throw r3;
     */
    @com.kt.jvm.JvmStatic
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static final <D extends Operation.Data, W> Response<W> parse(okio.BufferedSource r7, Operation<D, W, ?> r8, ScalarTypeAdapters r9) throws java.io.IOException {
        /*
            java.lang.String r0 = "source"
            com.kt.jvm.internal.Intrinsics.checkParameterIsNotNull(r7, r0)
            java.lang.String r0 = "operation"
            com.kt.jvm.internal.Intrinsics.checkParameterIsNotNull(r8, r0)
            java.lang.String r0 = "scalarTypeAdapters"
            com.kt.jvm.internal.Intrinsics.checkParameterIsNotNull(r9, r0)
            com.apollographql.apollo.api.internal.json.BufferedSourceJsonReader r0 = new com.apollographql.apollo.api.internal.json.BufferedSourceJsonReader
            r0.<init>(r7)
            java.io.Closeable r0 = (java.io.Closeable) r0
            r1 = 0
            r2 = 0
            java.lang.Throwable r2 = (java.lang.Throwable) r2
            r3 = r0
            com.apollographql.apollo.api.internal.json.BufferedSourceJsonReader r3 = (com.apollographql.apollo.api.internal.json.BufferedSourceJsonReader) r3     // Catch:{ all -> 0x0044 }
            r4 = 0
            r3.beginObject()     // Catch:{ all -> 0x0044 }
            com.apollographql.apollo.api.internal.json.ResponseJsonStreamReader r5 = new com.apollographql.apollo.api.internal.json.ResponseJsonStreamReader     // Catch:{ all -> 0x0044 }
            r6 = r3
            com.apollographql.apollo.api.internal.json.JsonReader r6 = (com.apollographql.apollo.api.internal.json.JsonReader) r6     // Catch:{ all -> 0x0044 }
            r5.<init>(r6)     // Catch:{ all -> 0x0044 }
            java.util.Map r5 = r5.toMap()     // Catch:{ all -> 0x0044 }
            if (r5 == 0) goto L_0x0031
            goto L_0x0035
        L_0x0031:
            java.util.Map r5 = com.kt.collections.MapsKt.emptyMap()     // Catch:{ all -> 0x0044 }
        L_0x0035:
            com.apollographql.apollo.api.SimpleOperationResponseParser r6 = com.apollographql.apollo.api.SimpleOperationResponseParser.INSTANCE     // Catch:{ all -> 0x0044 }
            com.apollographql.apollo.api.Response r6 = r6.parse(r5, r8, r9)     // Catch:{ all -> 0x0044 }
            r0.close()
            return r6
        L_0x0044:
            r3 = move-exception
            r2 = r3
            throw r3     // Catch:{ all -> 0x0048 }
        L_0x0048:
            r3 = move-exception
            r0.close()     // Catch:{ all -> 0x0051 }
            goto L_0x0052
        L_0x0051:
            r4 = move-exception
        L_0x0052:
            throw r3
        */
        throw new UnsupportedOperationException("Method not decompiled: com.apollographql.apollo.api.SimpleOperationResponseParser.parse(okio.BufferedSource, com.apollographql.apollo.api.Operation, com.apollographql.apollo.api.ScalarTypeAdapters):com.apollographql.apollo.api.Response");
    }

    private final <D extends Operation.Data, W> Response<W> parse(Map<String, ? extends Object> map, Operation<D, W, ?> operation, ScalarTypeAdapters scalarTypeAdapters) {
        D d;
        List errors;
        Object obj = map.get(OperationServerMessage.Data.TYPE);
        if (!(obj instanceof Map)) {
            obj = null;
        }
        Map responseData = (Map) obj;
        if (responseData != null) {
            d = operation.responseFieldMapper().map(new SimpleResponseReader(responseData, operation.variables(), scalarTypeAdapters));
        } else {
            d = null;
        }
        Object obj2 = map.get("errors");
        if (!(obj2 instanceof List)) {
            obj2 = null;
        }
        Iterable responseErrors = (List) obj2;
        if (responseErrors != null) {
            Iterable<Map> $this$map$iv = responseErrors;
            Collection destination$iv$iv = new ArrayList(CollectionsKt.collectionSizeOrDefault($this$map$iv, 10));
            for (Map errorPayload : $this$map$iv) {
                destination$iv$iv.add(INSTANCE.readError(errorPayload));
            }
            errors = (List) destination$iv$iv;
        } else {
            errors = null;
        }
        W wrapData = operation.wrapData(d);
        Object obj3 = map.get(ApolloOperationMessageSerializer.JSON_KEY_EXTENSIONS);
        Map map2 = (Map) (!(obj3 instanceof Map) ? null : obj3);
        return new Response<>(operation, wrapData, errors, null, false, map2 != null ? map2 : MapsKt.emptyMap(), null, 88, null);
    }

    private final Error readError(Map<String, ? extends Object> map) {
        Iterator<Map.Entry<String, ? extends Object>> it;
        List list;
        String str;
        String message = "";
        List locations = CollectionsKt.emptyList();
        Map customAttributes = new LinkedHashMap();
        Iterator<? extends Map.Entry<String, ?>> it2 = map.entrySet().iterator();
        while (it2.hasNext()) {
            Map.Entry<String, ? extends Object> next = it2.next();
            String key = next.getKey();
            Object value = next.getValue();
            int hashCode = key.hashCode();
            if (hashCode != -1197189282) {
                if (hashCode != 954925063) {
                    it = (Iterator<Map.Entry<String, ? extends Object>>) it2;
                } else if (key.equals("message")) {
                    if (value == null || (str = value.toString()) == null) {
                        str = "";
                    }
                    message = str;
                    it = (Iterator<Map.Entry<String, ? extends Object>>) it2;
                    it2 = it;
                } else {
                    it = (Iterator<Map.Entry<String, ? extends Object>>) it2;
                }
            } else if (key.equals("locations")) {
                Iterable locationItems = (List) (!(value instanceof List) ? null : value);
                if (locationItems != null) {
                    Iterable<Map> $this$map$iv = locationItems;
                    Collection destination$iv$iv = new ArrayList(CollectionsKt.collectionSizeOrDefault($this$map$iv, 10));
                    for (Map it3 : $this$map$iv) {
                        destination$iv$iv.add(INSTANCE.readErrorLocation(it3));
                        it2 = it2;
                    }
                    it = (Iterator<Map.Entry<String, ? extends Object>>) it2;
                    list = (List) destination$iv$iv;
                } else {
                    it = (Iterator<Map.Entry<String, ? extends Object>>) it2;
                    list = CollectionsKt.emptyList();
                }
                locations = list;
                it2 = it;
            } else {
                it = (Iterator<Map.Entry<String, ? extends Object>>) it2;
            }
            customAttributes.put(key, value);
            it2 = it;
        }
        return new Error(message, locations, customAttributes);
    }

    private final Error.Location readErrorLocation(Map<String, ? extends Object> map) {
        long line = -1;
        long column = -1;
        if (map != null) {
            for (Map.Entry<String, ? extends Object> entry : map.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                int hashCode = key.hashCode();
                if (hashCode != -1354837162) {
                    if (hashCode == 3321844 && key.equals("line")) {
                        if (value != null) {
                            line = ((Number) value).longValue();
                        } else {
                            throw new TypeCastException("null cannot be cast to non-null type com.kt.Number");
                        }
                    }
                } else if (!key.equals("column")) {
                    continue;
                } else if (value != null) {
                    column = ((Number) value).longValue();
                } else {
                    throw new TypeCastException("null cannot be cast to non-null type com.kt.Number");
                }
            }
        }
        return new Error.Location(line, column);
    }
}
